﻿<?php

//////////////////////////////////////////////////////////////////////////
//
// WikiHiero2 - A PHP convert from text using "Manual for the encoding of
// hieroglyphic texts for computer input" syntax to HTML entities (table and
// images).
//
// Copyright (C) 2004-2010 Guillaume Blanchard (Aoineko)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
//////////////////////////////////////////////////////////////////////////

require "wh_HTML/wh_config.php";
require "wh_HTML/wh_dico.php";
require "wh_HTML/wh_list.php";

////////////////////////////////////////////////////////////////////////////////
/**
 *
 */
class WH_HTML extends WH_Plugin
{
	protected $output;


	////////////////////////////////////////////////////////////////////////////////
	/**
	 * Constructor
	 */
	function __construct()
	{
		// Plugin parameters
		$this->RegisterVariable("Scale(%)", WH_Variable::Integer, 100);
		$this->RegisterVariable("ShowLines", WH_Variable::Boolean, false);
	}

	public static function CanExport() { return true; }
	public static function CanDisplay() { return true; }
	
	public static function GetName() { return "HTML"; }
	public static function GetId() { return "HTML"; }
	public static function GetVersion() { return "0.2"; }
	public static function GetDesc($type = WH_Plugin::TypeDefault, $lang = "") { return "HTML output (Table & CSS)"; }
	public static function GetCredit($lang = "")
	{
		$ret = "Guillaume Blanchard (Aoineko) under GPL (2004-2010). ";
		$ret .= "Hieroglyphs credit: S. Rosmorduc, G. Watson, J. Hirst (under GFDL).";
		return $ret;
	}
	
	////////////////////////////////////////////////////////////////////////////////
	/**
	 *
	 */
	public function &Export(&$document)
	{
		$this->depth = 0;
		$this->output = "";
		
		$this->output .= "(avant)";
		$this->ExportNode($document->GetRoot());
		$this->output .= "(apres)";
		
		return $this->output;
	}

	////////////////////////////////////////////////////////////////////////////////
	/**
	 *
	 */
	/*protected function IsLayoutSubdivide(&$node)
	{
		return (($node != null) && ($node->GetClass() == WH_CLASS_LAYOUT) && ($node->IsAttributeSet(WH_Attribute::Operator)) && ($node->GetAttribute(WH_Attribute::Operator) == "subdiv"));
	}*/
	
	////////////////////////////////////////////////////////////////////////////////
	/**
	 *
	 */
	/*protected function IsLayoutJuxtaposition(&$node)
	{
		return (($node != null) && ($node->GetClass() == WH_CLASS_LAYOUT) && ($node->IsAttributeSet(WH_Attribute::Operator)) && ($node->GetAttribute(WH_Attribute::Operator) == "juxtapos"));
	}*/
	
	////////////////////////////////////////////////////////////////////////////////
	/**
	 *
	 */
	protected function ComputeHeight(&$node)
	{
		global $wh_files;
		
		$size = 0;
		if($node->GetClass() == WH_CLASS_HIEROGLYPH)
		{
			$code = $node->GetAttribute(WH_Attribute::Code);
			if(array_key_exists($code, $wh_files))
				$size = $wh_files[$code]["height"];
		}
		else if($node->GetClass() == WH_CLASS_LAYOUT)
		{
			$op = $node->GetAttribute(WH_Attribute::Operator);
			foreach($node->GetChildren() as $child)
			{
				$temp = $this->ComputeHeight($child);
				switch($op)
				{
				case WH_OPERATOR_SUBORDINATION:
					$size += $temp;
					break;
				case WH_OPERATOR_JUXTAPOSITION:
				case WH_OPERATOR_LIGATURE:
					$size = max($size, $temp);
					break;
				}			
			}
		}
		return $size;
	}
	
	////////////////////////////////////////////////////////////////////////////////
	/**
	 *
	 */
	protected function ComputePrefabs(&$node, &$text)
	{
		global $wh_files;
		
		if($node->GetClass() == WH_CLASS_HIEROGLYPH)
		{
			if($text != "")
				$text .= "&";
			$text .= $node->GetAttribute(WH_Attribute::Code);
		}
		else if($node->GetClass() == WH_CLASS_LAYOUT)
		{
			foreach($node->GetChildren() as $child)
				$this->ComputeHeight($child, $text);
		}
	}

	////////////////////////////////////////////////////////////////////////////////
	/**
	 *
	 */
	protected function ExportHiero(&$node, $scale = 1.0)
	{
		global $wh_files;
		
		$size = $this->ComputeHeight($node);
		if($node->GetClass() == WH_CLASS_HIEROGLYPH)
		{
			$size *= $scale;
			$this->output .= "<img style='padding:0; margin:0;' height='$size' src='plugins/wh_HTML/img/hiero_".$node->GetAttribute(WH_Attribute::Code).".png'/>";
		}
		else if($node->GetClass() == WH_CLASS_LAYOUT)
		{
			$text = "";
			$this->ComputePrefabs($node, $text);
			if(array_key_exists($text, $wh_files)) // find prefabs
			{
				$this->output .= "<img style='padding:0; margin:0;' src='plugins/wh_HTML/img/hiero_".$wh_files[$text]["file"].".png'/>";
			}
			else // manual computation
			{
				$scale = min(1.0, 38.0 / $size);			
				foreach($node->GetChildren() as $child)
					$this->ExportHiero($child, $scale);
			}
		}
	}
	
	////////////////////////////////////////////////////////////////////////////////
	/**
	 *
	 */
	protected function ExportNode(&$node)
	{
		if($node->GetClass() == WH_CLASS_LINE)
		{
			$this->output .= "<table cellspacing='0' cellpadding='0' style='display:inline-table; padding:0; margin:0;'>";
			$this->output .= "<tr style='padding:0;  margin:0;'>\n";
			foreach($node->GetChildren() as $child)
			{
				$this->output .= "<td style='padding:0; margin:0;'>";
				$this->ExportHiero($child);
				$this->output .= "</td>\n";
			}
			$this->output .= "</tr></table>\n";
		}
		
		foreach($node->GetChildren() as $child)
			$this->ExportNode($child);
		
		/*if($this->IsLayoutSubdivide($node))
			$this->output .= "<table style=\"display:inline;\">\n";
		if($this->IsLayoutJuxtaposition($node))
			$this->output .= "<table style=\"display:inline;\">\n<tr>";
			
		if($this->IsLayoutSubdivide($node->GetParent()))
			$this->output .= "<tr><td>";
		if($this->IsLayoutJuxtaposition($node->GetParent()))
			$this->output .= "<td>";
							
		if(($node->GetClass() == WH_CLASS_HIEROGLYPH) && ($node->IsAttributeSet(WH_Attribute::Code)))
			$this->output .= "<img src=\"plugins/wh_HTML/img/hiero_".$node->GetAttribute(WH_Attribute::Code).".png\"/>\n";

		foreach($node->GetChildren() as $child)
			$this->ExportNode($child);
			
		if($this->IsLayoutSubdivide($node->GetParent()))
			$this->output .= "</td></tr>\n";
		if($this->IsLayoutJuxtaposition($node->GetParent()))
			$this->output .= "</td>\n";
			
		if($this->IsLayoutSubdivide($node))
			$this->output .= "</table>\n";
		if($this->IsLayoutJuxtaposition($node))
			$this->output .= "</tr></table>\n";*/
	}

	////////////////////////////////////////////////////////////////////////////////
	/**
	 *
	 */
 	public function Display(&$output)
	{
		echo $output;
	}
}

?>